package com.workoutgrabber.ui.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.swing.JButton;
import javax.swing.JTextField;

import org.joda.time.DateTime;
import org.netbeans.validation.api.builtin.Validators;
import org.netbeans.validation.api.ui.ValidationGroup;
import org.netbeans.validation.api.ui.ValidationPanel;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.toedter.calendar.JDateChooser;
import com.workoutgrabber.model.CardioExercise;
import com.workoutgrabber.ui.listeners.ExerciseCreatedEvent;
import com.workoutgrabber.ui.listeners.ExerciseCreatedListener;

public class CardioExercisePanel extends ExercisePanel implements ActionListener {
	private JTextField textFieldExcerciseName, textFieldDuration;
	private JDateChooser cardioExerciseDate;
	private JButton buttonAddCardioExercise;
	private ValidationPanel validationPanel;
	private ValidationGroup validationGroup;
	
	public CardioExercisePanel(){
		super();
		
		initComponents();
		initValidation();
		buildPanel();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == buttonAddCardioExercise){			
			fireExerciseCreatedEvent();
		}	
	}
	
//-----------------
// private methods
//-----------------

	private void initValidation(){
		validationPanel = new ValidationPanel();	
		validationGroup = validationPanel.getValidationGroup();
		validationGroup.add(textFieldExcerciseName, Validators.REQUIRE_NON_EMPTY_STRING);
		validationGroup.add(textFieldDuration, Validators.REQUIRE_NON_NEGATIVE_NUMBER, Validators.REQUIRE_VALID_NUMBER);		
	}
	
	private void initComponents() {
		textFieldExcerciseName = new JTextField();
		textFieldExcerciseName.setName("Name");
		
		textFieldDuration = new JTextField();
		textFieldDuration.setName("Duration");
		
		cardioExerciseDate = new JDateChooser();
		
		buttonAddCardioExercise	= new JButton("Add exercise");
		buttonAddCardioExercise.addActionListener(this);
	}
	
	private void buildPanel() {
		String rowDefinition = "p, 5dlu, p, 3dlu, p, 5dlu, p, 10dlu, p";
		
		FormLayout layout = new FormLayout(WorkoutPanel.COLUMN_DEFINITION, rowDefinition.toString());

		PanelBuilder builder = new PanelBuilder(layout);
		CellConstraints cc = new CellConstraints();		
		
		builder.addSeparator("Cardio excercise", cc.xyw(1, 1, 7));
		
		builder.addLabel("Name", cc.xy (1, 3));
		builder.add(textFieldExcerciseName, cc.xy (3, 3));
		builder.addLabel("Duration", cc.xy (5, 3));
		builder.add(textFieldDuration, cc.xy (7, 3));
		
		builder.addLabel("Date", cc.xy(1, 5));
		builder.add(cardioExerciseDate, cc.xy (3, 5));
		builder.add(buttonAddCardioExercise, cc.xy(7, 5));
		
		builder.add(validationPanel, cc.xyw(1, 7, 7));
		
		add(builder.getPanel());
	}
	
	private void fireExerciseCreatedEvent(){		
		if(listeners == null)
			listeners = new LinkedList<ExerciseCreatedListener>();
		
		if( validationGroup.validateAll() == null ){
			String name = textFieldExcerciseName.getText();
			DateTime date = new DateTime(cardioExerciseDate.getDate());
			Double duration = Double.valueOf(textFieldDuration.getText()); 					

			ExerciseCreatedEvent event = new ExerciseCreatedEvent(this, 0, CardioExercise.getInstance(name, date, duration));

			for (ExerciseCreatedListener listener : listeners) {
				listener.exerciseCreated(event);
			}
		}
	}
}
